//
//  RBSearchTagView.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/11.
//

#import "RBSearchTagView.h"
#import "RBTagContainerView.h"
#import "RBDefine.h"
#import <Masonry.h>
#import "RanTool.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "UIView+RBAdd.h"
#import "RBThemeConfig.h"

@interface RBSearchTagView()

@property(nonatomic, strong)RBTagContainerView *containerView;

@end

@implementation RBSearchTagView

- (void)setData:(NSArray *)data
{
    [self.containerView addTagViews:({
        NSMutableArray *arrayM = NSMutableArray.array;
        for (NSString *title in data)
        {
            [arrayM addObject:[self itemViewWithTitle:title]];
        }
        arrayM;
    })];
    [self.containerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.containerView.calulatedHeight);
    }];
}

- (void)clickTitle:(UIGestureRecognizer *)tap {
    NSString *title = tap.view.rbExData;
    if (self.block) {
        self.block(title);
    }
}

- (UIView *)itemViewWithTitle: (NSString *)title
{
    UIView *view = UIView.new;
    view.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
    view.height = Zoom(26);
    [view rbSetCornerRadius:Zoom(13)];
    view.rbExData = title;
    [view rbAddTapGesWithTarget:self action:@selector(clickTitle:)];
    CGFloat padding = Zoom(15);
    
    UILabel *textL = UILabel.new;
    textL.text = title;
    textL.font = [UIFont systemFontOfSize:Zoom(12)];
    textL.textColor = [RBThemeConfig textBlackColor];
    [view addSubview:textL];
    [textL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(padding);
        make.right.mas_equalTo(-padding);
    }];
    
    view.width = padding * 2 + [RanTool sizeWithText:textL.text consSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:textL.font].width + 2;
    return view;
}

- (RBTagContainerView *)containerView
{
    if (!_containerView)
    {
        self.containerView = ({
            RBTagContainerView *view = RBTagContainerView.new;
            CGFloat width = kRBMainScreenWidth - Zoom(11)*2;
            view.constraintWidth = width;
            view.vMargin = Zoom(10);
            view.hMargin = Zoom(10);
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(0);
                make.width.mas_equalTo(width);
                make.top.bottom.mas_equalTo(0);
                make.height.mas_equalTo(0);
            }];
            view;
        });
    }
    return _containerView;
}

@end
